package com.zkingsoft.wechat;


import java.util.Arrays;
import java.util.List;

import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.matrix.core.anotations.RemoveRequestToken;
import com.matrix.core.pojo.AjaxResult;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.web.BaseAction;
import com.zkingsoft.bean.BizActivities;
import com.zkingsoft.bean.BizUser;
import com.zkingsoft.redis.RedisUserLoginUtils;
import com.zkingsoft.service.BizActivitiesService;
import com.zkingsoft.service.BizCollageService;

/**
 * @description (活动)
 * @author 何明
 * @date 2018-05-23 00:08
 */
@Controller
@RequestMapping(value = "wxapi/activies")
public class WxActivitiesAction extends BaseAction{

	@Autowired
	private BizActivitiesService bizActivitiesService;
	@Autowired
	private BizCollageService bizCollageService;
	@Autowired
	RedisUserLoginUtils redisUserLoginUtils;
	
	/**
	 * 分页查询商家所有活动
	 * 1:活动管理，不用传UserId
	 * 2:用户查看某商户活动，传UserId
	 */   
   	@RequestMapping(value =  "/queryAll")
	public @ResponseBody AjaxResult queryAll(@RequestBody BizActivities bizActivities) {
   		BizUser loginUser = redisUserLoginUtils.getLoginUser();
   		if(StringUtils.isBlank(bizActivities.getUserId())){
   			bizActivities.setUserId(loginUser.getOpenId());
   		}
	 	return new AjaxResult(AjaxResult.STATUS_SUCCESS, bizActivitiesService.queryInPage(bizActivities),bizActivitiesService.queryTotalRecord(bizActivities));
	}
   	/**
	 * 查询所有活动
	 */   
   	@RequestMapping(value =  "/queryAllActivities")
	public @ResponseBody AjaxResult queryAllActivities(@RequestBody BizActivities bizActivities) {
   		
	 	return new AjaxResult(AjaxResult.STATUS_SUCCESS, 
	 			bizActivitiesService.queryInPage(bizActivities),
	 			bizActivitiesService.queryTotalRecord(bizActivities));
	}
	/**
	 * 查询活动详情
	 */   
   	@RequestMapping(value =  "/queryDetail/{keys}")
	public @ResponseBody AjaxResult queryDetail(@PathVariable("keys") String actId) {
		//BizUser loginUser = redisUserLoginUtils.getLoginUser();
		BizActivities bizActivities=bizActivitiesService.queryById(actId);
	 	return new AjaxResult(AjaxResult.STATUS_SUCCESS,Arrays.asList(bizActivities));
	}
   	
   	/**
	 * 上下架活动
	 */   
   	@RequestMapping(value =  "/upperOrLower")
	public @ResponseBody AjaxResult upperOrLower(@RequestBody BizActivities bizActivities) {
		BizUser loginUser = redisUserLoginUtils.getLoginUser();
		bizActivities.setUserId(loginUser.getOpenId());
		int i=bizActivitiesService.upperOrLower(bizActivities);
		if(i > 0){
	 		return new AjaxResult(AjaxResult.STATUS_SUCCESS, "操作成功");
	 	}else {
	 		return new AjaxResult(AjaxResult.STATUS_FAIL, "操作失败");
		}
   	
   	}
   	

}